package com.sise.foodplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sise.foodplatform.pojo.QueryInfo;
import com.sise.foodplatform.pojo.Restaurant;
import com.sise.foodplatform.service.impl.RestaurantServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: tae
 * @Date: 2021/4/12 21:12
 * @Description: 餐馆管理
 */

@RestController
public class RestaurantController {

    @Autowired
    private RestaurantServiceImpl restaurantService;

    @Configuration
    public class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/pic/**").addResourceLocations("file:C:\\\\user\\\\XXX\\\\");
        }
    }

    //获得全部餐馆
    @RequestMapping("/allRestaurant")
    public String getRestaurantList(QueryInfo queryInfo){
        //获取最大列表数和当前编号
        int numbers = restaurantService.getRestaurantCounts("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Restaurant> restaurants = restaurantService.getAllRestaurant("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("restaurants",restaurants);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    //更改餐馆状态
    @RequestMapping("/restaurantState")
    public String updateRestaurantState(@RequestParam("res_id")Integer id, @RequestParam("state")Boolean state){
        int i = restaurantService.updateRestaurantState(id, state);
        return i>0?"success":"error";
    }

    //获得某用户的全部餐馆
    @RequestMapping("/allRestaurantByUser")
    public String getRestaurantListByUser(@RequestParam("id") int userId,@RequestParam(value = "query",required = false)String query,
                                          @RequestParam("pageNum")int pageNum,@RequestParam("pageSize")int pageSize){
        //获取最大列表数和当前编号
        int numbers = restaurantService.getRestaurantCountsByUserId("%" + query + "%",userId);
        int pageStart = (pageNum - 1) * pageSize;
        List<Restaurant> restaurants = restaurantService.getAllRestaurantByUserId("%" + query + "%", pageStart,
                pageSize,userId);
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("restaurants",restaurants);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }
    //用户添加餐馆
    @RequestMapping("/addRestaurant")
    public String addRestaurant(@RequestBody Restaurant restaurant){
        System.out.println(restaurant.toString());
        restaurant.setResState(0);
        int i = restaurantService.addRestaurantByUser(restaurant);
        return i>0?"success":"error";
    }

    //用户删除餐馆
    @RequestMapping("/deleteRestaurant")
    public String deleteRestaurant(int id){
        int i = restaurantService.deleteRestaurantById(id);
        return i>0?"success":"error";
    }

    //
    @RequestMapping("/getUpdateRestaurant")
    public String getUpdateUser(int id){
        Restaurant restaurant = restaurantService.getUpdateRestaurant(id);
        String s = JSON.toJSONString(restaurant);
        System.out.println("成功");
        System.out.println(restaurant.toString());
        return s;
    }

    //用户修改餐馆
    @RequestMapping("/editRestaurant")
    public String editRestaurant(@RequestBody Restaurant restaurant){
        int i = restaurantService.editRestaurantById(restaurant);
        return i>0?"success":"error";
    }

    //按喜爱程度获取餐馆
    @RequestMapping("/allRestaurantByLike")
    public String getAllRestaurantByLike(){
        //获取最大列表数和当前编号
        List<Restaurant> restaurants = restaurantService.getAllRestaurantByLike();
        HashMap<String, Object> res = new HashMap<>();
        res.put("restaurants",restaurants);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/uploadFile")
    @ResponseBody
    public Object uploadFile(@RequestParam("file") MultipartFile file){
        JSONObject jsonObject = new JSONObject();
        if (file.isEmpty()){
            jsonObject.put("msg","文件为空");
            jsonObject.put("code",0);
        }
        //获取文件名
        String o = file.getOriginalFilename();
        String Filename1 = System.currentTimeMillis() +o;
        //获取路径
        String path = getPath();
//        //创建新的文件
//        File fileMkdir = new File(path+"\\pic");
//        if (!fileMkdir.exists()){
//            fileMkdir.mkdir();
//        }
        try {
            File fileMkdir = new File(path+"\\"+Filename1);
            //写入文件
            file.transferTo(fileMkdir);
            jsonObject.put("msg","ok");
            jsonObject.put("code",200);
            jsonObject.put("avator",Filename1);
        } catch (IOException e) {
            e.printStackTrace();
            jsonObject.put("msg","图片上传失败");
            jsonObject.put("code",0);
        }
        return jsonObject;
    }

    /**
     * 获取存储路径
     */
    private String getPath(){
        //获取当前项目路径
        String c = System.getProperty("user.dir");
        //设置文件存储路径
        //return c+"\\src\\main\\resources\\static\\fileTemplate"+"\\";
        //E:\vue_workspace\deliciousfood\src\assets\pic
        return "E:\\vue_workspace\\deliciousfood\\src\\assets\\pic";
    }
}
